package com.uama.plugin.provider;

import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider;
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.icons.AllIcons;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.util.CommonProcessors;
import com.uama.plugin.service.MyJavaService;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Objects;

public class MyLineMarkerProvider extends RelatedItemLineMarkerProvider {

    @Override
    protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo> result) {
        if (element instanceof PsiNameIdentifierOwner && element instanceof PsiMethod) {
            PsiMethod psiMethod = (PsiMethod) element;
            CommonProcessors.CollectProcessor<PsiMethod> processor = new CommonProcessors.CollectProcessor<>();
            MyJavaService.getInstance(element.getProject()).process(psiMethod, processor);
            Collection<PsiMethod> results = processor.getResults();
            if (!results.isEmpty()) {
                NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(AllIcons.Ide.Rating)
                        .setTargets(results)
                        .setTooltipText("Navigate to provider");
                result.add(builder.createLineMarkerInfo(Objects.requireNonNull(((PsiNameIdentifierOwner) element).getNameIdentifier())));
            }
        }
    }
}
